<?php
class PostsController extends AppController {
	var $name = 'Posts';
	
	function beforeFilter() {
		
	}
	
	function beforeRender() {
		
	}
	
	function index() {
		$postsByFind =  $this->Post->find('all');
		$postsByQuery = $this->Post->query(" select * from posts as Post; ");
		
		echo "postsByFind: ";
		print_r($postsByFind);
		echo "postsBuQuery: ";
		print_r($postsByQuery);
		
		$this->set('posts', $postsByFind);
		//$this->set('posts', $this->Post->query(" select * from posts; "));
	}
	
	function view($id = null, $user = null) {
		pr($this->params);
		$this->Post->id = $id;
		
		//$readPost = $this->Post->query("select * from posts as Post where id = . $id;");
		$readPostByRead = $this->Post->read();
		
		print_r($readPostByRead);
		
		$this->set('post', $readPostByRead);
	}
	
	function add() {
		pr($this->data);
		if (!empty($this->data)) {
			if ($this->Post->save($this->data)) {
				$this->Session->setFlash('Your post has been saved.');
				$this->redirect(array('action' => 'index'));
				pr($this->data);
			}
		}
	}
	
	function delete($id) {
	    $this->Post->delete($id);
	    $this->Session->setFlash('The post with id: '.$id.' has been deleted.');
	    $this->redirect(array('action'=>'index'));
	}
	
	function edit($id = null) {		
	    $this->Post->id = $id;
	    if (empty($this->data)) {
	        $this->data = $this->Post->read();
	    } else {
	        if ($this->Post->save($this->data)) {
	            $this->Session->setFlash('Your post has been updated.');
	            $this->redirect(array('action' => 'index'));
	        }
	    }
	}
}
?>